/*<![CDATA[*/
/*]]>*/

var app = angular.module('app', []);
app.controller('MainController', CRUD);

function doAction($scope, $http, url, method, data) {
    $scope.message = '';
    $http({
        url : url,
        method : method,
        headers : {
            'Content-Type' : 'application/x-www-form-urlencoded'
        },
        data : data
    }).success(function(r) {
        /*alert(r.msg)*/
        $scope.message = r.msg;
        /*if (!r.success) {
        $scope.message = r.message;
        return;
        }*/
        //window.location.href = '/success';
    });
}

function CRUD($rootScope, $scope, $http) {
    $scope.message = '';
    $scope.id = '1235';
    $scope.name = '香蕉';
    $scope.price = '20.00';

    //添加：POST
    $scope.getProducts = function() {
        doAction($scope, $http, '/products', 'get', '');
    }

    //添加：POST
    $scope.addProduct = function() {
        var data = 'id=' + $scope.id
            + '&name=' + $scope.name
            + '&price=' + $scope.price;
        doAction($scope, $http, '/product', 'POST', data);
    }

    //更新：PUT
    $scope.updateProducts = function() {
        var data = 'id=' + $scope.id
            + '&name=' + $scope.name
            + '&price=' + $scope.price;
        doAction($scope, $http, '/' + $scope.id, 'PUT', data);
    }

    //删除：DELETE
    $scope.deleteProduct = function() {
        doAction($scope, $http, '/' + $scope.id, 'delete', '');
    }
}